<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\Goods;
use App\Http\Requests\Admin\GoodsController\doRule;

class GoodsController extends Controller
{
    public function index()
    {
        $result = [
            'result'=>Goods::lists(),
            'sear_value' => \request()->get('sear_value'),
            'is_opend' => \request()->get('is_opend'),
        ];

        return view('admin.goods.index', $result);
    }

    public function add()
    {
        $result = [

        ];

        return view('admin.goods.edit',$result);
    }

    public function edit(Request $request, $id)
    {
        $result = [
            'id'=>$id,
            'result'=> Goods::info($id),
        ];

        return view( 'admin.goods.edit', $result);
    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $goods_img = !empty($request->file('goods_img')) ? $request->file('goods_img') : [];

            if (!empty($goods_img)) {

                $path = $goods_img->store('images/'. date('Ymd'));

                $input['goods_img'] = "/upload/".$path;

            } else {

                unset($input['goods_img']);

            }

            if (empty($input['id'])) {
                unset($input['id']);
                Goods::add($input);
            } else {
                Goods::edit($input['id'],$input);
            }

            return self::success([]);

        }
    }

    public function del($id)
    {

        Goods::del($id);

        return self::success([]);

    }
}
